package com.itheima.goods.controller;

import com.changgou.goods.pojo.Brand;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.entity.StatusConstant;
import com.itheima.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 版权：(C) 版权所有 2000-2020 上海天好电子商务有限公司苏州分公司
 * <简述>
 * <详细描述>
 *
 * @author LiuShanshan
 * @version $Id$
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     *<简述>    查
     *<详细描述>
     * @author Liushanshan
     * @param
     * @return com.itheima.entity.Result<com.changgou.goods.pojo.Brand>
    */
    @RequestMapping("/getAll")
    public Result<Brand> getAll(){
        List<Brand> brandList = brandService.findAll();
        Result<Brand> brandResult = new Result<>(true, StatusCode.OK, "查询成功", brandList);
        return brandResult;
    }

    @RequestMapping("/getOne/{id}")
    public Result<Brand> getOne(@PathVariable("id") Integer id){
        Brand brand = brandService.findById(id);
        Result<Brand> brandResult = new Result<>(true, StatusCode.OK, "查询成功", brand);
        return brandResult;
    }

    /**
     *<简述>   增加
     *<详细描述>
     * @author Liushanshan
     * @param
     * @return com.itheima.entity.Result<com.changgou.goods.pojo.Brand>
    */
    @RequestMapping("/add")
    public Result<Brand> add(){
        Result<Brand> brandResult;
        Brand brand = new Brand();
        brand.setId(100000);
        brand.setImage("sdfa");
        brand.setLetter("J");
        brand.setName("Mapper保存测试");
        brand.setSeq(12);
        Integer brandStatus = brandService.add(brand);
        if(Objects.equals(StatusConstant.ONE, brandStatus)){
            brandResult  = new Result<>(true, StatusCode.OK, "增加成功");
        }else {
            brandResult  = new Result<>(false, StatusCode.ERROR, "增加失败");
        }
        return brandResult;
    }

    /**
     *<简述>     删除
     *<详细描述>
     * @author Liushanshan
     * @param id
     * @return com.itheima.entity.Result<com.changgou.goods.pojo.Brand>
    */
    @RequestMapping("/deleteById/{id}")
    public Result<Brand> deleteById(@PathVariable("id") Integer id){
        Result<Brand> brandResult;
        Integer brandStatus = brandService.deleteById(id);
        if(Objects.equals(StatusConstant.ONE, brandStatus)){
            brandResult  = new Result<>(true, StatusCode.OK, "删除成功");
        }else {
            brandResult  = new Result<>(false, StatusCode.ERROR, "删除失败");
        }
        return brandResult;
    }

    /**
     *<简述>     更新
     *<详细描述>
     * @author Liushanshan
     * @param id
     * @return com.itheima.entity.Result<com.changgou.goods.pojo.Brand>
     */
    @RequestMapping("/update")
    public Result<Brand> update(){
        Result<Brand> brandResult;
        Integer brandStatus = brandService.update(new Brand());
        if(Objects.equals(StatusConstant.ONE, brandStatus)){
            brandResult  = new Result<>(true, StatusCode.OK, "删除成功");
        }else {
            brandResult  = new Result<>(false, StatusCode.ERROR, "删除失败");
        }
        return brandResult;
    }

    /**
     *<简述>     分页
     *<详细描述>
     * @author Liushanshan
     * @param id
     * @return com.itheima.entity.Result<com.changgou.goods.pojo.Brand>
     */
    @RequestMapping("/page/{page}/{size}")
    public Result<PageInfo<Brand>> page(@PathVariable("page") int page, @PathVariable("size") int size){
        PageInfo<Brand> brandPageInfo = brandService.staticPage(page, size);
        return new Result<>(true, StatusCode.OK, "删除成功", brandPageInfo);
    }

    /**
     *<简述>  post请求url上加入动态路径
     *<详细描述>
     * @author Liushanshan
     * @param page
     * @param size
     * @param brand
     * @return void
    */
    @PostMapping("/conditionPage/{page}/{size}")
    public void conditionPage(@PathVariable("page") int page, @PathVariable("size") int size,
                              @RequestBody(required = false) Brand brand){
        int a = 10/0;
        // 获取post请求，url上的动态路径和方法体内的对象
        System.out.println("page:" + page);
        System.out.println("size:" + size);
        System.out.println("brand:" + brand);
    }

}
